const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");


// 开票界面 应收应付抵扣票据
router.post('/bill/getYsfList', async ctx => {
    let transType = ctx.request.body.transType
    let buId = ctx.request.body.buId
    let billId = ctx.request.body.billId
    let tenantId = ctx.user.tenantId

    let trans = ' '
    if (transType === 150501) {
        trans = ` and a.transType=153406 `
    }
    if (transType === 150601) {
        trans = ` and a.transType=153407 `
    }
    let hx = ',0 hxAmount '
    let left = ''
    if(billId) {
        hx = `,isnull(c.hxAmount,0) hxAmount `
        left = ` left join (select * from invoice_dikou where iid=${billId} ) c on a.id=c.relId `
    }
    let sql = `select a.id,a.id relId,a.buId,b.name buName,a.transTypeName,a.billDate,a.billNo,
    a.leftAmount  ${hx}  from invoice a inner join contact b on a.buId=b.id and a.tenantId=${tenantId} and buId=${buId} 
 ${trans} and a.billState='accounted' and isnull(a.leftAmount,0)<>0 
${left}
`
    let ret = await db.query(sql, {type: QueryTypes.SELECT})
    return ctx.suc('', ret )

})

module.exports = router;